package com.customer.crm.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.customer.crm.system.pojo.DictPojo;
import com.customer.crm.system.pojo.Dict_Type_Pojo;
import com.customer.crm.system.service.DictIService;
import com.customer.crm.system.service.DictTypeIService;
import com.customer.crm.util.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class List_Dict_Controller {
    @Resource
    private DictIService dictIService;
  @Resource
  private DictTypeIService dictTypeIService;

    //通过字典类型按钮展示字典数据
    @ResponseBody
    @RequestMapping("/Dict_findByDictTypeId")
    public ResultData dictPageByTypeId(@RequestParam("dictType.id") String typeId, long page, long rows, ModelMap modelMap) {
        List<DictPojo> dt = dictIService.dictAllByTypeIdAndTypeCode(typeId, page-1, rows);
        modelMap.addAttribute("result", dt);
        modelMap.addAttribute("totalRows", dt.size());
        ResultData resultData = ResultData.buildFailureResult("data", modelMap);
        return resultData;
    }
    //通过字典类型展示字典数据
    @ResponseBody
    @RequestMapping("/Dict_findByDictAll")
    public ResultData Dict_findByDictAll( long page, long rows, ModelMap modelMap){
        Page<DictPojo> dictPage = new Page<>(page, rows);
        Page<DictPojo> page1 = dictIService.page(dictPage, null);
        modelMap.addAttribute("totalRows", page1.getTotal());
        modelMap.addAttribute("result", page1.getRecords());
        ResultData resultData = ResultData.buildSuccessResult("data", modelMap);
        return  resultData;
    }

    //删除按钮事件处理函数
    @ResponseBody
    @RequestMapping("/deleteDict")
    public ModelMap deleteDict(@RequestParam("ids")String[] ids){
        ModelMap modelMap = new ModelMap();
        for (String id:ids
             ) {
            boolean b = dictIService.removeById(id);
            if (b==true){
                modelMap.addAttribute("message","删除成功");
            }else{
                modelMap.addAttribute("message","删除成功");
            }
        }
    return modelMap;
    }

   //修改按钮事件处理函数
    @RequestMapping("/toUpdateDict")
    public String toUpdateDict(@RequestParam("dict.id")String id,ModelMap modelMap){
        DictPojo byId = dictIService.getById(id);

        modelMap.addAttribute("dictPojo",byId);
      //通过dict对象拿到外键，之后利用外键查询dictType所对应的数据
        String typeId = byId.getTypeId();
        Dict_Type_Pojo typePojo = dictTypeIService.getById(typeId);

        modelMap.addAttribute("typePojo",typePojo);
        //给词条类型下拉框填充选项
        List<Dict_Type_Pojo> list = dictTypeIService.list();
        modelMap.addAttribute("list",list);
        return "views/system/dict/update_dict";
    }
    //保存
    @ResponseBody
    @RequestMapping("/updateDict")
    public ModelMap updateDict(DictPojo dictPojo){
        ModelMap modelMap = new ModelMap();
        boolean b = dictIService.updateById(dictPojo);
        modelMap.addAttribute("success",b);
        return modelMap;
    }

    //添加
    @RequestMapping("/toInsertDict")
    public String toInsertDict(@RequestParam("dictType.id")String dictTypeId,ModelMap modelMap){
        //给词条类型下拉框填充选项
        List<Dict_Type_Pojo> list = dictTypeIService.list();
        modelMap.addAttribute("list",list);
        return "/views/system/dict/insert_dict";
    }
    @ResponseBody
    @RequestMapping("/insertDict")
    public ModelMap insertDict(DictPojo dictPojo){
        ModelMap modelMap = new ModelMap();
        boolean b = dictIService.save(dictPojo);
        modelMap.addAttribute("success",b);
        return modelMap;
    }




}
